import re
import sys
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from mysql import MyMysql
import datetime
from PySide6.QtWidgets import (QWidget, QApplication, QVBoxLayout, QFormLayout, QDockWidget,QTableWidget,QTableWidgetItem,QCheckBox,QStackedWidget,
                               QLabel, QFrame, QLineEdit, QPushButton, QMainWindow, QMessageBox)
from PySide6.QtGui import QGuiApplication, Qt, QPalette, QFont, QAction

class RegAndLogin(QMainWindow):
    def __init__(self):
        super().__init__()
        self.sql = MyMysql()
        # self.resize(800, 600)
        self.main_widget = QWidget()
        self.main_widget.setStyleSheet('background-color:#EEE9E9;border-radius:5px')
        self.setCentralWidget(self.main_widget)
        self.set_ui()


    def set_ui(self, scale: float = 0.75):
        screen =QGuiApplication.primaryScreen().size()
        width = int(screen.width()*scale)
        height = int(screen.height()*scale)
        self.resize(width, height)
        self.main_widget.setFixedSize(int(350), int(290))
        print(self.setAutoFillBackground(True))  # None

        self.setStyleSheet("background-color:#00BFFF")
        # 设置新窗口
        self.setWindowTitle('登录注册窗口')
        v_layout = QVBoxLayout(self.main_widget)
        v_layout.setAlignment(Qt.AlignVCenter)
        form_layout = QFormLayout()
        label = QLabel('798管理平台')
        font = QFont()
        font.setPointSize(17)
        label.setFont(font)
        form_layout.addRow(label)
        # 设置颜色为灰色
        palette = label.palette()
        palette.setColor(QPalette.WindowText, Qt.gray)
        label.setPalette(palette)

        #  创建一条水平线
        line = QFrame()
        line.setFrameShape(QFrame.Shape.HLine)
        line.setFrameShadow(QFrame.Shadow.Sunken)
        line.setStyleSheet('background-color:rgb(200, 200, 198)')
        line.setFixedHeight(1)  # 设置水平线的高度
        # 将水平线加到主窗口的中心布局中
        form_layout.addRow(line)

        self.name = QLineEdit('')
        self.name.setPlaceholderText('用户名')
        self.name.setStyleSheet('background-color:#FFFFFF;color:gray;border:none;padding-left:5px;border-radius:5px')
        self.name.setFixedHeight(20)
        # 图标
        icon = QIcon('用户.png')
        u_action = QAction(self.name)
        u_action.setIcon(icon)
        self.name.addAction(u_action, QLineEdit.ActionPosition.TrailingPosition)
        form_layout.addRow(self.name)


        self.password = QLineEdit('')
        self.password.setPlaceholderText("密码")
        self.password.setEchoMode(QLineEdit.Password)   # 密码加密
        self.password.setStyleSheet('background-color:#FFFFFF;color:gray; border:none;padding-left:5px;border-radius:5px')
        self.password.setFixedHeight(20)
        # 图标
        icon = QIcon('密码.png')
        u_action = QAction(self.password)
        u_action.setIcon(icon)
        self.password.addAction(u_action, QLineEdit.ActionPosition.TrailingPosition)
        form_layout.addRow(self.password)

        self.again_pwd = QLineEdit('')
        self.again_pwd.setPlaceholderText("再次密码")
        self.again_pwd.setEchoMode(QLineEdit.Password)  # 密码加密
        self.again_pwd.setStyleSheet(
            'background-color:#FFFFFF;color:gray; border:none;padding-left:5px;border-radius:5px')
        self.again_pwd.setFixedHeight(20)
        # 图标
        icon = QIcon('密码.png')
        u_action = QAction(self.again_pwd)
        u_action.setIcon(icon)
        self.again_pwd.addAction(u_action, QLineEdit.ActionPosition.TrailingPosition)
        form_layout.addRow(self.again_pwd)


        self.phone = QLineEdit('')
        self.phone.setPlaceholderText('手机号')
        self.phone.setStyleSheet('background-color:#FFFFFF;color:gray;border:none;padding-left:5px;border-radius:5px')
        self.phone.setFixedHeight(20)
        form_layout.addRow(self.phone)

        reg_btn = QPushButton('注册')
        reg_btn.setStyleSheet("background-color: #00BFFF;color:white")
        reg_btn.setFixedHeight(40)
        form_layout.addRow(reg_btn)
        v_layout.addLayout(form_layout)
        # self.RegAndLogin = RegAndLogin()
        reg_btn.clicked.connect(self.register)

        # self.button = QPushButton()
        # self.button.setIcon(QIcon('28感叹号-圆框.png'))  # 替换为你的图标路径
        # self.button.setText("请输入用户名和密码")
        # # 设置样式表，使图标和文本在按钮中居中
        # self.button.setStyleSheet("QPushButton {""text-align:center;""color:gray;"" icon-size: 18px;""}")
        # form_layout.addWidget(self.button)

        # 设置行间距
        form_layout.setHorizontalSpacing(20)
        form_layout.setVerticalSpacing(20)
        form_layout.addRow(form_layout)

    def resizeEvent(self, event):
        super().resizeEvent(event)  # 调用基类的resizeEvent进行必要的处理
        # 计算新位置以保持组件居中
        width = self.main_widget.width()
        height = self.main_widget.height()
        x = (self.width() - width) // 2
        y = (self.height() - height) // 2
        # 设置组件的新位置
        self.main_widget.move(x, y)

    def register(self):
        """实现注册"""
        #1. 输入的内容
        current_time = datetime.datetime.now()
        uame = self.name.text()
        pwd = self.password.text()
        conform_pwd = self.again_pwd.text()
        phone = self.phone.text()
        if pwd != conform_pwd:
            QMessageBox.warning(self,"密码不一样","两次密码输入不一致")
        if uame == "":
            QMessageBox.warning(self,"警告","用户名不能为空")
        if phone == "":
            QMessageBox.warning(self,"警告","手机号不能为空")
        if not re.match(r"1[3-9]\d{9}",phone):
            QMessageBox.warning(self,"警告","手机号格式不正确")

        # 写入数据库
        user_ = {
            "username": uame,
            "password": pwd,
            "phone": phone,
            "date": current_time
        }
        rest = self.sql.insert_one_user(**user_)
        if rest:
            QMessageBox.information(self,"成功","注册成功")
            # 关闭页面
            self.hide()
        else:
            QMessageBox.warning(self, "失败", "注册失败")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = RegAndLogin()
    window.show()
    sys.exit(app.exec())

